/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.sergivonavi.materialbanner.utils;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;

/**
 * 自定义属性工具类
 */
public class AttrUtils {
    /**
     * 获取自定义的string属性
     *
     * @param attrSet attrSet
     * @param name name
     * @param defaultValue defaultValue
     * @return String
     */
    public static String getString(AttrSet attrSet, String name, String defaultValue) {
        String value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getStringValue();
        }
        return value;
    }

    /**
     * 获取自定义的Integer属性
     *
     * @param attrSet attrSet
     * @param name name
     * @param defaultValue defaultValue
     * @return Integer
     */
    public static Integer getInteger(AttrSet attrSet, String name, int defaultValue) {
        int value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getIntegerValue();
        }
        return value;
    }

    /**
     * 获取自定义的boolean属性
     *
     * @param attrSet attrSet
     * @param name name
     * @param defaultValue defaultValue
     * @return boolean
     */
    public static boolean getBoolean(AttrSet attrSet, String name, boolean defaultValue) {
        boolean value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getBoolValue();
        }
        return value;
    }

    /**
     * 获取自定义的color属性的int值
     *
     * @param attrSet attrSet
     * @param name name
     * @param defaultValue defaultValue
     * @return int
     */
    public static int getColor(AttrSet attrSet, String name, int defaultValue) {
        int value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getColorValue().getValue();
        }
        return value;
    }

    /**
     * 获取自定义的Element属性
     *
     * @param attrSet attrSet
     * @param name name
     * @param defaultValue defaultValue
     * @return Element
     */
    public static Element getElement(AttrSet attrSet, String name, Element defaultValue) {
        Element value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getElement();
        }
        return value;
    }
}